package com.yyx.jupiter.component;

import jakarta.annotation.Resource;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class UserExposeBloomFilter {
    @Resource
    RedisClient redisClient;

    // 定义用于判断元素是否存在于布隆过滤器的Lua脚本
    private static final String BF_MEXISTS_SCRIPT = "return redis.call('BF.MEXISTS', KEYS[1], unpack(ARGV))";

    // 判断元素是否可能存在于布隆过滤器中
    @SuppressWarnings({"unchecked", "rawtypes"})
    public List<Boolean> isBFExists(String userId, List<String> videoIds) {
        RedisScript<List<Long>> script = new DefaultRedisScript(BF_MEXISTS_SCRIPT, List.class);
        List<String> args = videoIds.stream().map(t -> userId + ":" + t).toList();
        List<Long> result = redisClient.executeBF(script, List.of("yyx:expose_bloom_filter"), args);
        return result.stream().map(t -> t == 1L).toList();
    }
}
